<template>
  <div class="app-container">
    <div class="filter-container" />

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row tooltip-effect="dark" style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="企业名称" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.companyName }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="法人名称">
        <template slot-scope="scope">
          <span>{{ scope.row.legalPerson }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="手机">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120px" align="center" label="人员规模">
        <template slot-scope="scope">
          <span>{{ scope.row.legalPerson }}</span>
        </template>
      </el-table-column>
      <el-table-column width="180px" align="center" label="注册时间">
        <template slot-scope="scope">
          <span>{{ scope.row.registeredTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120px" align="center" label="注册资金(万元)">
        <template slot-scope="scope">
          <span>{{ scope.row.registeredCapital }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120px" align="center" label="主营业务" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.mainBusiness }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120px" align="center" label="上游产业" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.upMainBusiness }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120px" align="center" label="下游产业" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.downMainBusiness }}</span>
        </template>
      </el-table-column>
      <el-table-column width="150px" align="center" label="2020年营业额(万元)">
        <template slot-scope="scope">
          <span>{{ scope.row.turnover1 }}</span>
        </template>
      </el-table-column>
      <el-table-column width="150px" align="center" label="2021年营业额(万元)">
        <template slot-scope="scope">
          <span>{{ scope.row.turnover2 }}</span>
        </template>
      </el-table-column>
      <el-table-column width="150px" align="center" label="2022年营业额(万元)">
        <template slot-scope="scope">
          <span>{{ scope.row.turnover3 }}</span>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="审核状态" width="150px" show-overflow-tooltip>
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.status | statusFilterText }}
          </el-tag>
          <span v-if="row.status==2" style="margin-left:10px">{{ row.failReason }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <!--      <el-table-column width="100px" label="Importance">-->
      <!--        <template slot-scope="scope">-->
      <!--          <svg-icon v-for="n in +scope.row.importance" :key="n" icon-class="star" class="meta-item__icon" />-->
      <!--        </template>-->
      <!--      </el-table-column>-->

      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <router-link :to="'/example/edit/'+scope.row.id">-->

          <el-button v-if="scope.row.status==0" type="primary" size="mini" icon="el-icon-edit" @click="updateStatus(scope.row.id,1)">
            通过
          </el-button>
          <el-button v-if="scope.row.status==0" type="danger" size="mini" icon="el-icon-delete" @click="updateStatus(scope.row.id,2)">
            拒绝
          </el-button>
          <!--          </router-link>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog title="审核" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item label="原因备注">
          <el-input v-model="temp.failReason" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button type="primary" @click="writeFailReason">
          {{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, updateStatus } from '@/api/company/company-examine'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'IntendedCustomerList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'info',
        2: 'danger'
      }
      return statusMap[status]
    },
    statusFilterText(status) {
      const statusMap = {
        1: '已通过',
        0: '待审核',
        2: '已拒绝'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      textMap: {
        update: '编辑',
        create: '新增'
      },
      temp: {
        id: undefined,
        failReason: '',
        status: 0
      },
      rules: {
        name: [{ required: true, message: '分类名称为必填项', trigger: 'blur' }]
      },
      dialogFormVisible: false,
      dialogStatus: '',
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {

    updateStatus(id, type) {
      this.temp.id = id
      this.temp.status = type
      if (type === 2) {
        this.dialogFormVisible = true
      } else {
        updateStatus(this.temp).then(response => {
          this.$notify({
            title: '成功',
            message: '审核成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      }
    },
    writeFailReason() {
      updateStatus(this.temp).then(response => {
        this.$notify({
          title: '成功',
          message: '审核成功',
          type: 'success',
          duration: 2000
        })
        this.dialogFormVisible = false
        this.getList()
      })
    },
    addData() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    getList() {
      this.listLoading = true
      this.listQuery.pageNum = this.listQuery.page
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
